package com.song.service.impl;


import com.song.dao.impl.ExpressDaoImpl;
import com.song.exception.expressException.ExpressCodeException;
import com.song.exception.expressException.ExpressNumberException;
import com.song.polo.Express;
import com.song.service.ExpressService;
import com.song.utils.DateFormatUtil;
import com.song.utils.RandomUtil;
import com.song.utils.TransformUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class ExpressServiceImpl implements ExpressService {
    @Resource
    private ExpressDaoImpl expressDao;

    /**
     * 查询快递控制台信息
     *
     * @return 信息的集合
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> expressConsole() {
        return expressDao.expressConsole();
    }

    /**
     * 查询所有的快递信息
     *
     * @param limit     判断是否分页 true 分页
     * @param pageIndex 分页时的起始索引
     * @param pageSize  每次分页的显示数据数
     * @return 快递信息集合
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Express> selectExpress(Boolean limit, Integer pageIndex, Integer pageSize) {
        List<Express> expresses = expressDao.selectExpress(limit, pageIndex, pageSize);
        for (Express express : expresses) {
            express.setInputTime1(express.getInputTime().toString());
            express.setOutputTime1(DateFormatUtil.getDate(express.getOutputTime()));
            express.setExpressStatus(express.getStatus() == 1 ? "待取" : "已取");
        }
        return expresses;
    }

    /**
     * 通过收件人手机号查询快速信息
     *
     * @param receiverPhone 收件人手机号
     * @return 收件人的所有快递信息
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Express> selectByPhone(String receiverPhone) {
        List<Express> expresses = expressDao.selectByPhone(receiverPhone);
        for (Express express : expresses) {
            express.setInputTime1(express.getInputTime().toString());
            express.setOutputTime1(DateFormatUtil.getDate(express.getOutputTime()));
        }
        return expresses;
    }

    /**
     * 通过验证码查询快递信息
     *
     * @param code 收件人的验证码
     * @return 快递信息对象
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Express selectByCode(String code) {
        Express express = expressDao.selectByCode(code);

        if (express != null){
            express.setInputTime1(express.getInputTime().toString());
            express.setOutputTime1(DateFormatUtil.getDate(express.getOutputTime()));
        }
        return express;
    }

    /**
     * 通过快递编号查询快递信息
     *
     * @param expressId 快递编号
     * @return 查询到的快递信息
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Express selectById(Integer expressId) {
        return expressDao.selectById(expressId);
    }


    /**
     * 通过快递单号查询快递
     *
     * @param expressNumber 快递单号
     * @return 快递信息
     */
    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Express selectByNumber(String expressNumber) {
        Express express = expressDao.selectByNumber(expressNumber);
        if (express != null){
            express.setInputTime1(express.getInputTime().toString());
            express.setOutputTime1(DateFormatUtil.getDate(express.getOutputTime()));
        }
        return express;
    }

    /**
     * 新增快递
     *
     * @param express 新增的快递信息对象
     * @return 1成功 0失败 -1快递单号重复
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer insertExpress(Express express) {
        try {
            String code = RandomUtil.getRandom();
            express.setCode(code);
            expressDao.insertExpress(express);
            //给收件人发送验证码
            System.out.println("您的取件码时:" + code);
            return 1;
        } catch (ExpressNumberException e) {
            if (e.getMessage().endsWith("for key 'express.expressNumber'")) {
                System.out.println("快递单号重复");
                return -1;
            } else {
                e.printStackTrace();
            }
        } catch (ExpressCodeException e) {
            if (e.getMessage().endsWith("for key 'express.code'")) {
                System.out.println("验证码重复");
                return insertExpress(express);
            } else {
                e.printStackTrace();
            }
        }
        return 0;
    }


    /**
     * 修改快速信息
     *
     * @param express 快递单号
     * @return 1成功 0失败  -1 订单号重复
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer updateExpress(Express express) {
        try {
            //如果收件人电话号码法改变，删除原来的数据，重新插入新的数据
            Express express1 = selectById(express.getExpressId());
            if (!express.getReceiverPhone().equals(express1.getReceiverPhone())) {
                deleteExpress(express.getExpressId());
                insertExpress(express);
            }
            //如果快递状态变为出库，添加出库时间
            if (express.getStatus() == 0) {
                express.setOutputTime(TransformUtil.getTimestamp());
                express.setCode(null);
            }
            expressDao.updateExpress(express);
            return 1;
        } catch (ExpressNumberException e) {
            if (e.getMessage().endsWith("for key 'express.expressNumber'")) {
                System.out.println("快递单号重复");
                return -1;
            } else {
                e.printStackTrace();
            }
        }
        return 0;

    }

    /**
     * 删除快速信息
     *
     * @param expressId 快递ID
     * @return 1成功 2失败
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public Integer deleteExpress(Integer expressId) {
        return expressDao.deleteExpress(expressId);
    }

    /**
     * 通过手机号查询未取快递
     *
     * @param userPhone 手机号
     * @param status    快递状态
     * @return 快递信息集合
     */
  /*  @Override
    public List<Express> selectExpress(String userPhone, Integer status) {
        List<Express> expresses = expressDao.selectExpress(userPhone, status);
        for (Express express : expresses) {
            express.setInputTime1(DateFormatUtil.getDate(express.getInputTime()));
            express.setOutputTime1(DateFormatUtil.getDate(express.getOutputTime()));
        }
        return expresses;
    }*/

    /**
     * 通过code更新快递状态
     *
     * @param code 快递验证码
     * @return true 成功
     */
   /* @Override
    public Boolean updateStatus(String code) {
        return expressDao.updateStatus(code);
    }*/

    /**
     * 通过手机号查询未取快递
     *
     * @param writerPhone 录入人手机号
     * @param status      快递状态
     * @return 快递信息集合
     */
    /*@Override
    public List<Express> selectExpressByWriterPhone(String writerPhone, Integer status) {
        List<Express> expresses = expressDao.selectExpressByWriterPhone(writerPhone, status);
        for (Express express : expresses) {
            //转换时间格式，变为String类型
            express.setInputTime1(DateFormatUtil.getDate(express.getInputTime()));
            express.setOutputTime1(DateFormatUtil.getDate(express.getOutputTime()));
        }
        return expresses;
    }*/
}
